package worldutils.huntgame.Utils;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import worldutils.huntgame.HuntGame;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class CompassUpdater {

    private static JavaPlugin plugin;

    public static void initialize(JavaPlugin pl) {
        plugin = pl;
    }
    public static void startCompassUpdater() {
        new BukkitRunnable() {
            @Override
            public void run() {
                for (UUID chaserUUID : TagUtils.playerTags.keySet()) {
                    if (TagUtils.getTag(Bukkit.getPlayer(chaserUUID)).equals(HuntGame.getMessage("hunter"))) {
                        Player chaser = Bukkit.getPlayer(chaserUUID);
                        if (chaser != null && chaser.isOnline()) {
                            Player nearestEscapee = getNearestEscapee(chaser);
                            if (nearestEscapee != null) {
                                chaser.setCompassTarget(nearestEscapee.getLocation());
                            }
                        }
                    }
                }
            }
        }.runTaskTimer(plugin, 0L, 20L); // 每秒更新一次（20 ticks）
    }

    private static Player getNearestEscapee(Player chaser) {
        Player nearest = null;
        double nearestDistance = Double.MAX_VALUE;

        for (UUID escapeeUUID : TagUtils.playerTags.keySet()) {
            Player escapee = Bukkit.getPlayer(escapeeUUID);
            if (escapee != null && escapee.isOnline() && TagUtils.getTag(escapee).equals(HuntGame.getMessage("runner"))) {
                double distance = chaser.getLocation().distance(escapee.getLocation());
                if (distance < nearestDistance) {
                    nearestDistance = distance;
                    nearest = escapee;
                }
            }
        }

        return nearest;
    }

    public static void stopCompassUpdater() {
        Bukkit.getScheduler().cancelTasks(plugin);
    }

    public static void startCompassNotificationTask() {
        HuntGame.compassNotificationTask = new BukkitRunnable() {
            @Override
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    if (TagUtils.getTag(player).equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                        String playerinfo = TagUtils.getCloseRangePlayer(HuntGame.getMessage("runner"),player);
                        player.sendActionBar(playerinfo);
                    }
                    if (TagUtils.getTag(player).equalsIgnoreCase(HuntGame.getMessage("runner"))) {
                        String playerinfo2 = TagUtils.getCloseRangehunterPlayer(HuntGame.getMessage("hunter"),player);
                        player.sendActionBar(playerinfo2);
                    }
                }
            }
        };
        HuntGame.compassNotificationTask.runTaskTimer(plugin, 0L, 20L); // 20 ticks = 1 second
    }

    public static void stopCompassNotificationTask() {
        if (HuntGame.compassNotificationTask != null) {
            HuntGame.compassNotificationTask.cancel();
            HuntGame.compassNotificationTask = null;
        }
    }
}